XSLT Notebook

Alex van Buitenen

This is not a XSLT tutorial or Quick Reference.
It is just my personal notebook for XSLT.

Header
Create Variable
Position binnen een loop
Insert a bookmark
Insert a reference to a bookmark in the current file
Selecting Nodes (XPath)
Output Escaping
Replace a string in xslt 1.0
Define a newline

Header

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
 

Create Variable

<xsl:variable name="m_LibName" select="/doc/assembly/name"/>

Position binnen een loop

<xsl:value-of select="position()"/>

Insert a bookmark

<a target="_self"> <!-- Insert a html link element -->
  <xsl:attribute name="name"> <!-- Add the name attribute -->
    <xsl:value-of select="$m_TypeName"/> <!-- specify the name of the anchor -->
    </xsl:attribute> <!-- end the attribute -->
  <xsl:value-of select="$m_TypeName"/> <!-- specify the text to be displayed -->
</a>
 

Insert a reference to a bookmark in the current file

<a target="_blank">
  <xsl:attribute name="href">
    #<xsl:value-of select="$m_TypeName"/>
    </xsl:attribute>
  <xsl:value-of select="$m_TypeName"/>
</a>
 

Selecting Nodes (XPath)

<xsl:value-of select="xpath expression"/>

XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below:

Expression Description  
nodename Selects all child nodes of the named node
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes


Output Escaping
 
 

Replace a string in xslt 1.0

<xsl:template name="string-replace-all">
  <xsl:param name="text" />
  <xsl:param name="replace" />
  <xsl:param name="by" />
  <xsl:choose>
    <xsl:when test="contains($text, $replace)">
      <xsl:value-of select="substring-before($text,$replace)" />
      <xsl:value-of select="$by" />
      <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="substring-after($text,$replace)" />
        <xsl:with-param name="replace" select="$replace" />
        <xsl:with-param name="by" select="$by" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text" />
    </xsl:otherwise>
    </xsl:choose>
</xsl:template>

calls as:

(Beware of the extra single quotes in the replace and by parameters, otherwise you will get a StackOverflow exception while calling string-replace-all.)

<xsl:variable name="newtext">
  <xsl:call-template name="string-replace-all">
    <xsl:with-param name="text" select="." />
    <xsl:with-param name="replace" select="'StringToReplace'" />
    <xsl:with-param name="by" select="'ReplaceBy'" />
  </xsl:call-template>
</xsl:variable>

<xsl:value-of select = "$newtext"/>

Define a newline.

<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>